package com.sinosoft.dsp.platform.components;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.primefaces.json.JSONArray;
import org.primefaces.json.JSONException;
import org.primefaces.json.JSONObject;

import com.sinosoft.dsp.platform.exception.SinoPlatformException;
import com.sinosoft.dsp.platform.utils.FacesContextUtils;
import com.sinosoft.dsp.system.core.exception.SinoException;
import com.sinosoft.dsp.system.utils.Const;

/**
 * 可编辑的ViewBean的抽象类
 * 
 * @author 潘巍（Peter Pan）
 * @since 2011-2-27 下午05:44:38
 */
public class AbstractEditViewBean extends AbstractViewBean {

	private Map<String, Object> fields = new HashMap<String, Object>();

	/**
	 * 返回值
	 */
	private JSONObject returnValue;

	/**
	 * 允许显示的按钮
	 */
	private List<String> renderedButtons;

	public Map<String, Object> getFields() {
		return fields;
	}

	public Object getField(String key) {
		return fields.get(key);
	}

	public void addField(String key, Object value) {
		fields.put(key, value);
	}

	/**
	 * 验证是否显示该按钮
	 * 
	 * @param btnName
	 * @return
	 */
	public boolean renderedButton(String btnName) {
		if (this.renderedButtons != null)
			for (String btn : this.renderedButtons)
				if (btn.equalsIgnoreCase(btnName))
					return true;

		return false;
	}

	public void setRenderedButtons(String[] renderedButtons) {
		if (this.renderedButtons == null) {
			this.renderedButtons = new ArrayList<String>();
		} else {
			while (!this.renderedButtons.isEmpty()) {
				this.renderedButtons.remove(0);
			}
		}
		if (renderedButtons != null)
			for (String btn : renderedButtons)
				if (!Const.isEmpty(btn))
					this.renderedButtons.add(btn);

	}

	public void addRenderedButton(String btnName) {
		if (this.renderedButtons == null) {
			this.renderedButtons = new ArrayList<String>();
		}
		if (!Const.isEmpty(btnName) && !renderedButton(btnName)) {
			this.renderedButtons.add(btnName);
		}
	}

	public String getReturnValue() {
		return (returnValue == null) ? "{}" : returnValue.toString();
	}

	public void setReturnValue(JSONObject returnValue) {
		this.returnValue = returnValue;
	}

	private void setValidate(boolean validate, String[] msgs)
			throws SinoPlatformException {
		if (returnValue == null) {
			returnValue = new JSONObject();
		}
		try {
			returnValue.put(IEditViewBean.VALIDATE_FLAG, validate);
			if (msgs != null && msgs.length > 0) {
				JSONArray msgArr = new JSONArray();
				for (String msg : msgs) {
					if (!Const.isEmpty(msg)) {
						msgArr.put(msg);
					}
				}
				returnValue.put(IEditViewBean.VALIDATE_MSG, msgArr);
			}

		} catch (JSONException e) {
			throw new SinoPlatformException("获得验证结果表示出现错误");
		}
	}

	public boolean isValidate() throws SinoPlatformException {
		try {
			return returnValue.getBoolean(IEditViewBean.VALIDATE_FLAG);
		} catch (JSONException e) {
			throw new SinoPlatformException("获得验证结果出现错误");
		}
	}

	/**
	 * 子类覆盖该方法
	 * 
	 * @param methodName
	 * @return
	 */
	public String[] validate(String methodName) {
		return null;
	}

	public void doValidate(String methodName) throws SinoPlatformException {
		String[] msgs = validate(methodName);

		if (msgs == null || msgs.length == 0) {
			// 判断返回验证是否正确的结果
			setValidate(true, null);
		} else {
			// 判断返回验证是否正确的结果
			setValidate(false, msgs);

			// 添加form作用域的消息
			for (String msg : msgs) {
				FacesContextUtils.addGlobalErrorFacesMessage(msg);
			}
		}

	}

	public void find() throws SinoException {
		doValidate(IEditViewBean.EDIT_VIEWBEAN_BUTTON_FIND);
	}

	public void merge() throws SinoException {
		doValidate(IEditViewBean.EDIT_VIEWBEAN_BUTTON_MERGE);
	}

	public void next(String viewBeanName) throws SinoException {
		doValidate(IEditViewBean.EDIT_VIEWBEAN_BUTTON_NEXT);
	}

	public void remove() throws SinoException {
		doValidate(IEditViewBean.EDIT_VIEWBEAN_BUTTON_REMOVE);
	}

	public void save() throws SinoException {
		doValidate(IEditViewBean.EDIT_VIEWBEAN_BUTTON_SAVE);
	}

	public void flush() throws SinoException {
		doValidate(IEditViewBean.EDIT_VIEWBEAN_BUTTON_FLUSH);
	}
}
